//第一步： 导入网络模块
import http from '@ohos.net.http';

import api from './index'

//封装GET请求
// 网络请求 是否基于promise
const get =  (url:string,data={})=> {
  // 把 'data' 拼接成 'str'
  let str = '';
  if(data){
     for(let key in data){
        str+=key+"="+data[key]+"&"
     }
     str = str.slice(0,str.length-1);
     console.log(str);
  }
  // 创建请求任务
  let httpRequest = http.createHttp();
  // 调用request方法返回promise
  return httpRequest.request(
    str ? url+"?"+str: url,  // 请求地址
    {
      method: http.RequestMethod.GET,// 请求方式
      connectTimeout: 60000,
      readTimeout: 60000,
      header: {
        'Content-Type': 'application/json' // 请求头信息
      }
    })
}

const joint = (data:object):string=>{
  let str = '';
  if(data){
    for(let key in data){
      str+=key+"="+data[key]+"&"
    }
    str = str.slice(0,str.length-1);
    return str;
  }
}

const request = (url:string,method: http.RequestMethod,data):Promise<object>=>{
  // console.log(data)
  let head ={};
  if (method === http.RequestMethod.POST) {
    head = { 'Content-Type': 'application/x-www-form-urlencoded' }
    data = joint(data);
  }else if(method === http.RequestMethod.GET){
    head={'Content-Type': 'application/json'}
  }
  return new Promise((resolve,reject)=>{
    let httpRequest = http.createHttp();
    let obj;
    httpRequest.request(
      url,
      {
        method: method,
        extraData: data,
        connectTimeout: 60000,
        readTimeout: 60000,
        header: head
      })
      .then((value: http.HttpResponse) => {
        if (value.responseCode === 200) {
          // console.log(JSON.parse(`${value.result}`))
          obj = JSON.parse(`${value.result}`);
        } else {
          obj.msg = '请求错误';
          obj.code = 400;
        }
        resolve(obj)
      }).catch(() => {
      obj.msg = '请求错误';
      obj.code = 400;
      reject(obj)
    });
  })
}

//封装POST请求
const post =  (url:string,data={})=> {
  // 创建请求任务
  let httpRequest = http.createHttp();
  // 调用request方法返回promise
  return httpRequest.request(
    url,  // 请求地址
    {
      method: http.RequestMethod.POST,// 请求方式
      extraData: data,// 传递参数给后台
      connectTimeout: 60000,
      readTimeout: 60000,
      header: {
        'Content-Type': 'application/x-www-form-urlencoded' // 请求头信息
      }
    })
}



//接口一：注册
// http://localhost:5000/api/user/register
export const RegisterFn = (data={})=> {
   return request(`${api.host}/api/user/register`,http.RequestMethod.POST,data)
}

//接口二：登录
// http://localhost:5000/api/user/login
export const LoginFn = (data={})=> {
  return request(`${api.host}/api/user/login`,http.RequestMethod.POST,data)
}

//接口三：用户列表
// http://localhost:5000/api/user/list
export const userList = (data={})=> {
  return request(`${api.host}/api/user/list`,http.RequestMethod.POST,data)
}

//接口四：搜索用户
// http://localhost:5000/api/user/search
export const userSearch = (data={})=> {
  return request(`${api.host}/api/user/search`,http.RequestMethod.POST,data)
}

//接口五：删除用户
// http://localhost:5000/api/user/remove
export const userRemove = (data={})=> {
  return request(`${api.host}/api/user/remove`,http.RequestMethod.POST,data)
}

//接口六：用户信息
// http://localhost:5000/api/user/info
export const userInfo = (data={})=> {
  return request(`${api.host}/api/user/info`,http.RequestMethod.POST,data)
}

//接口七：添加好友
// http://localhost:5000/api/friend/add
export const friendAdd = (data={})=> {
  return request(`${api.host}/api/friend/add`,http.RequestMethod.POST,data)
}

//接口八：添加好友请求
// http://localhost:5000/api/friend/notice
export const friendNotice = (data={})=> {
  return request(`${api.host}/api/friend/notice`,http.RequestMethod.POST,data)
}

//接口九：处理好友请求
// http://localhost:5000/api/friend/modify
export const friendModify = (data={})=> {
  return request(`${api.host}/api/friend/modify`,http.RequestMethod.POST,data)
}

//接口十：删除好友
// http://localhost:5000/api/friend/remove
export const friendRemove = (data={})=> {
  return request(`${api.host}/api/friend/remove`,http.RequestMethod.POST,data)
}

//接口十一：通讯录好友列表
// http://localhost:5000/api/friend/list
export const friendList = (data={})=> {
  return request(`${api.host}/api/friend/list`,http.RequestMethod.POST,data)
}

//接口十一：判断是否为好友
// http://localhost:5000/api/friend/is_friend
export const friendIs = (data={})=> {
  return request(`${api.host}/api/friend/is_friend`,http.RequestMethod.POST,data)
}

//接口十二：添加聊天信息(发送信息)
// http://localhost:5000/api/message/add
export const messageAdd = (data={})=> {
  return request(`${api.host}/api/message/add`,http.RequestMethod.POST,data)
}

//接口十三：聊天信息列表
// http://localhost:5000/api/message/list
export const messageList = (data={})=> {
  return request(`${api.host}/api/message/list`,http.RequestMethod.POST,data)
}

//接口十四：删除聊天
// http://localhost:5000/api/message/remove
export const messageRemove = (data={})=> {
  return request(`${api.host}/api/message/remove`,http.RequestMethod.POST,data)
}

//接口十五：聊天信息列表（首页）
// http://localhost:5000/api/message/items
export const messageItems = (data={})=> {
  return request(`${api.host}/api/message/items`,http.RequestMethod.POST,data)
}




